home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Produtividade / OpenOffice.org 2.0.1 / openofficeorg3.cab / linecache.py < prev    next >
Text File  |  2005-11-19  |  3KB  |  101 lines

  1. """Cache lines from files.
  2.  
  3. This is intended to read lines from modules imported -- hence if a filename
  4. is not found, it will look down the module search path for a file by
  5. that name.
  6. """
  7.  
  8. import sys
  9. import os
  10.  
  11. __all__ = ["getline", "clearcache", "checkcache"]
  12.  
  13. def getline(filename, lineno):
  14.     lines = getlines(filename)
  15.     if 1 <= lineno <= len(lines):
  16.         return lines[lineno-1]
  17.     else:
  18.         return ''
  19.  
  20.  
  21. # The cache
  22.  
  23. cache = {} # The cache
  24.  
  25.  
  26. def clearcache():
  27.     """Clear the cache entirely."""
  28.  
  29.     global cache
  30.     cache = {}
  31.  
  32.  
  33. def getlines(filename):
  34.     """Get the lines for a file from the cache.
  35.     Update the cache if it doesn't contain an entry for this file already."""
  36.  
  37.     if filename in cache:
  38.         return cache[filename][2]
  39.     else:
  40.         return updatecache(filename)
  41.  
  42.  
  43. def checkcache():
  44.     """Discard cache entries that are out of date.
  45.     (This is not checked upon each call!)"""
  46.  
  47.     for filename in cache.keys():
  48.         size, mtime, lines, fullname = cache[filename]
  49.         try:
  50.             stat = os.stat(fullname)
  51.         except os.error:
  52.             del cache[filename]
  53.             continue
  54.         if size != stat.st_size or mtime != stat.st_mtime:
  55.             del cache[filename]
  56.  
  57.  
  58. def updatecache(filename):
  59.     """Update a cache entry and return its list of lines.
  60.     If something's wrong, print a message, discard the cache entry,
  61.     and return an empty list."""
  62.  
  63.     if filename in cache:
  64.         del cache[filename]
  65.     if not filename or filename[0] + filename[-1] == '<>':
  66.         return []
  67.     fullname = filename
  68.     try:
  69.         stat = os.stat(fullname)
  70.     except os.error, msg:
  71.         # Try looking through the module search path.
  72.         basename = os.path.split(filename)[1]
  73.         for dirname in sys.path:
  74.             # When using imputil, sys.path may contain things other than
  75.             # strings; ignore them when it happens.
  76.             try:
  77.                 fullname = os.path.join(dirname, basename)
  78.             except (TypeError, AttributeError):
  79.                 # Not sufficiently string-like to do anything useful with.
  80.                 pass
  81.             else:
  82.                 try:
  83.                     stat = os.stat(fullname)
  84.                     break
  85.                 except os.error:
  86.                     pass
  87.         else:
  88.             # No luck
  89. ##          print '*** Cannot stat', filename, ':', msg
  90.             return []
  91.     try:
  92.         fp = open(fullname, 'rU')
  93.         lines = fp.readlines()
  94.         fp.close()
  95.     except IOError, msg:
  96. ##      print '*** Cannot open', fullname, ':', msg
  97.         return []
  98.     size, mtime = stat.st_size, stat.st_mtime
  99.     cache[filename] = size, mtime, lines, fullname
  100.     return lines
  101.